package com.selune.online.ordering.system.project.customer.controller;

import com.selune.online.ordering.system.project.api.base.response.Response;
import com.selune.online.ordering.system.project.api.pojo.user.customer.dto.CustomerAddressDTO;
import com.selune.online.ordering.system.project.api.pojo.user.customer.vo.CustomerAddressVO;
import com.selune.online.ordering.system.project.api.service.customer.ICustomerAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @author xiaoyp
 * @date 2020/3/22
 * <p>
 * Description:
 */

@RestController
@RequestMapping("/api/v1/ordering/customer/user/address")
@Slf4j
@Api("收货地址控制器")
public class CustomerAddressController {
	
	@Autowired
	private ICustomerAddressService iCustomerAddressService;
	
	@ApiOperation(value = "添加收货地址", notes = "添加收货地址")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long", paramType = "query"),
		@ApiImplicitParam(name = "customerAddressDTO", value = "地址信息DTO", required = true, dataType = "CustomerAddressDTO", paramType = "body")
	})
	@PostMapping(value = "/save/{userId}", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<Boolean> saveAddress(@PathVariable("userId") Long userId,
	                                     @RequestBody CustomerAddressDTO customerAddressDTO) {
		return iCustomerAddressService.saveAddress(userId, customerAddressDTO);
	}
	
	@ApiOperation(value = "根据id更新收货地址", notes = "根据id更新收货地址")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "收货地址id", required = true, dataType = "Long", paramType = "query"),
		@ApiImplicitParam(name = "customerAddressDTO", value = "地址信息DTO", required = true, dataType = "CustomerAddressDTO", paramType = "body")
	})
	@PostMapping(value = "/update/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<Boolean> updateAddress(@PathVariable("id") Long id, @RequestBody CustomerAddressDTO customerAddressDTO) {
		return iCustomerAddressService.updateAddress(id, customerAddressDTO);
	}
	
	@ApiOperation(value = "查看用户下的所有地址", notes = "查看用户下的所有地址")
	@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long", paramType = "query")
	@GetMapping(value = "/list/{userId}", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<List<CustomerAddressVO>> addressList(@PathVariable("userId") Long userId) {
		return iCustomerAddressService.addressList(userId);
	}
	
	@ApiOperation(value = "根据id删除收货地址", notes = "根据id删除收货地址")
	@ApiImplicitParam(name = "id", value = "收货地址id", required = true, dataType = "Long", paramType = "query")
	@DeleteMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<Boolean> deleteAddressById(@PathVariable("id") Long id) {
		return iCustomerAddressService.deleteAddressById(id);
	}
	
	@ApiOperation(value = "根据id批量删除收货地址", notes = "根据id批量删除收货地址")
	@ApiImplicitParam(name = "ids", value = "收货地址ids", required = true, dataType = "List", paramType = "query")
	@DeleteMapping(value = "/delete/batch", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<Boolean> deleteAddressByIds(List<Long> ids) {
		return iCustomerAddressService.deleteAddressByIds(ids);
	}
}
